﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MachineStatusClient
{
    public partial class DJSearchForm : Form
    {
        BContentToSqlServer bts = new BContentToSqlServer();
        private string time;
        public DJSearchForm()
        {
            InitializeComponent();

            
            this.MinimizeBox = false; // 隐藏最小化按钮
            this.MaximizeBox = false; // 隐藏最大化按钮
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void DJSeachForm_Load(object sender, EventArgs e)
        {
            //BindWorkOrdersToDataGridView();
            BindWorkOrdersToDataGridView1();
        }

        public void SetMachineId(string id)
        {
            time = id;
        }
        private void BindWorkOrdersToDataGridView()
        {
            try
            {
                // 1. 从配置读取设备编码
                //string machineId = ConfigurationManager.AppSettings["DeviceCode"];
                dataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridView3.ColumnHeadersHeight = 45;
                //if (string.IsNullOrEmpty(machineId))
                //{
                //    MessageBox.Show("设备编码未配置！");
                //    return;
                //}

                // 2. 调用接口获取数据
                List<DJDataModelList> workOrders = bts.GetDJList();

                // 3. 绑定到DataGridView
                if (workOrders != null && workOrders.Count > 0)
                {
                    // 配置列映射（字段名 -> 列名）
                    //dataGridView1.AutoGenerateColumns = false; // 禁止自动生成列
                    // 设置开始时间和结束时间列的格式
                    //if (dataGridView3.Columns.Contains("Column5"))
                    //{
                    //    dataGridView3.Columns["Column5"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                    //    dataGridView3.Columns["Column5"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    //}
                    //if (dataGridView3.Columns.Contains("Column6"))
                    //{
                    //    dataGridView3.Columns["Column6"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                    //    dataGridView3.Columns["Column6"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    //}
                    // 绑定数据源
                    dataGridView3.DataSource = workOrders;

                    dataGridView3.RowTemplate.Height = 45;
                    //Message.Text = "";
                }
                else
                {
                    //MessageBox.Show("未查询到工单数据");
                    //Message.Text = "未查询到工单排程数据";
                    //Message.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show($"加载数据失败: {ex.Message}");
                //Message.Text = $"加载数据失败: {ex.Message}";
                //Message.ForeColor = Color.Red;
            }
        }
        private void BindWorkOrdersToDataGridView1()
        {
            try
            {
                // 1. 从配置读取设备编码
                //string machineId = ConfigurationManager.AppSettings["DeviceCode"];
                dataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridView3.ColumnHeadersHeight = 45;
                //if (string.IsNullOrEmpty(machineId))
                //{
                //    MessageBox.Show("设备编码未配置！");
                //    return;
                //}

                // 2. 调用接口获取数据
                List<DJDataModelList> workOrders = bts.GetDJList1(time);

                // 3. 绑定到DataGridView
                if (workOrders != null && workOrders.Count > 0)
                {
                    // 配置列映射（字段名 -> 列名）
                    //dataGridView1.AutoGenerateColumns = false; // 禁止自动生成列
                    // 设置开始时间和结束时间列的格式
                    //if (dataGridView3.Columns.Contains("Column5"))
                    //{
                    //    dataGridView3.Columns["Column5"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                    //    dataGridView3.Columns["Column5"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    //}
                    //if (dataGridView3.Columns.Contains("Column6"))
                    //{
                    //    dataGridView3.Columns["Column6"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                    //    dataGridView3.Columns["Column6"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    //}
                    if (dataGridView3.Columns.Contains("Column27"))
                    {
                        dataGridView3.Columns["Column27"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                        dataGridView3.Columns["Column27"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }
                    // 绑定数据源
                    dataGridView3.DataSource = workOrders;

                    dataGridView3.RowTemplate.Height = 45;
                    //Message.Text = "";
                }
                else
                {
                    //MessageBox.Show("未查询到工单数据");
                    //Message.Text = "未查询到工单排程数据";
                    //Message.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show($"加载数据失败: {ex.Message}");
                //Message.Text = $"加载数据失败: {ex.Message}";
                //Message.ForeColor = Color.Red;
            }
        }
    }
}
